﻿Imports System.Windows.Forms
Imports ArfaUI
Imports MiyzanResto.ClassAll.Class

Public Class TrxOrderJualPage
    Inherits PageTemplate
    Private Sub RefreshTabel()
        cmbMeja.Items.Clear()
        Dim dt As New DataTable
        If Utils.executeSP("proc_tabel", New Object() {"select", 0, 0, 0, 0}, dt) Then
            If dt IsNot Nothing Then
                If dt.Rows.Count > 0 Then
                    For Each dr As DataRow In dt.Rows
                        Dim nbtn As New Button
                        If dr("aktif").ToString = "1" Then
                            nbtn.BackColor = Color.Green
                        Else
                            nbtn.BackColor = Color.CornflowerBlue
                        End If
                        nbtn.ForeColor = Color.White
                        cmbMeja.Items.Add(dr("idtabel").ToString)
                        nbtn.Size = New System.Drawing.Size(72, 59)
                        nbtn.Text = dr("idtabel").ToString
                        nbtn.Name = dr("idtabel").ToString
                        nbtn.Location = New Point(dr("xpos"), dr("ypos"))
                        AddHandler nbtn.Click, AddressOf ClickMeja
                        AddHandler nbtn.MouseHover, AddressOf HoverMeja
                        'AddHandler nbtn.MouseUp, AddressOf UpEvent
                        'AddHandler nbtn.MouseMove, AddressOf MoveEvent

                        Dim ctlAll As Control()
                        ctlAll = PanButton.Controls.Find(dr("idtabel").ToString, False)
                        If ctlAll.Length > 0 Then
                            Dim ctldim As Button
                            ctldim = CType(ctlAll(0), Button)
                            ctldim.Location = New Point(dr("xpos"), dr("ypos"))
                            If dr("aktif").ToString = "1" Then
                                ctldim.BackColor = Color.Green
                            Else
                                ctldim.BackColor = Color.CornflowerBlue
                            End If
                            PanButton.Refresh()
                        Else
                            PanButton.Controls.Add(nbtn)
                        End If

                    Next
                End If
            End If
        End If
    End Sub

    Private Sub ClickMeja(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim btndim As New Button
        btndim = CType(sender, Button)
        If btndim.BackColor = Color.CornflowerBlue Then
            cmbMeja.Text = btndim.Text
            GridTransaksi.Rows.Clear()
            TabControl1.SelectedTab = TabPage2
            'btndim.BackColor = Color.Green
            'If Utils.exec_SP("proc_tabel", New Object() {"setaktif", btndim.Text, Convert.DBNull, Convert.DBNull, 1}) Then

            'End If
        ElseIf btndim.BackColor = Color.Green Then
            GridTransaksi.Rows.Clear()
            For i As Integer = 0 To cmbMeja.Items.Count - 1
                If cmbMeja.Items(i) = btndim.Text Then
                    cmbMeja.SelectedIndex = i
                    Exit For
                End If
            Next

            TabControl1.SelectedTab = TabPage2
        End If
    End Sub

    Private Sub viewBill(ByVal tabelid As String)
        Dim dt As New DataTable
        Dim otid As String = ""
        Dim dtfunc As New DataTable
        If Utils.GetFunction("getidtrxopen", New Object() {tabelid}, dtfunc) Then
            If Utils.RowExist(dtfunc) Then
                otid = dtfunc(0)(0)
            End If
        End If
        If otid = "" Then
            Exit Sub
        End If

        dt = GetAllEqual("vviewbill", "idtrx", otid)

        Dim printer As New ClsGeneralPrint()
        Dim dataprint As String = ""
        dataprint = String.Empty
        'cetakHeader()
        dataprint = String.Empty
        Dim iLeftMargin As Integer = 0
        If IsNumeric(My.Settings.LeftMargin) Then
            iLeftMargin = My.Settings.LeftMargin
        End If

        Dim iTopMargin As Integer = 0
        If IsNumeric(My.Settings.TopMargin) Then
            iTopMargin = My.Settings.TopMargin
        End If

        Dim iLoop As Integer = 0

        'While iLoop < iTopMargin
        '    dataprint = dataprint & Environment.NewLine
        '    iLoop += 1
        'End While
        Dim tanggaljam As String = ""
        tanggaljam = Now().ToString("yyyy-MMM-dd hh:mm:ss")
        Dim nama As String = ""
        Dim alamat As String = ""
        Dim notelp As String = ""
        Dim dthead As New DataTable
        Dim fax As String = ""
        If Utils.executeSP("proc_master_profil", New Object() {"select", Convert.DBNull, Convert.DBNull, Convert.DBNull, Convert.DBNull}, dthead) Then
            If dthead IsNot Nothing Then
                If dthead.Rows.Count > 0 Then
                    nama = dthead.Rows(0)("nama")
                    alamat = dthead.Rows(0)("alamat")
                    notelp = dthead.Rows(0)("telepon")
                    fax = dthead.Rows(0)("fax")
                End If
            End If
        End If
        'header
        'openDraw()
        dataprint &= Strings.Space(iLeftMargin) & printer.FillCenter(nama, 31, " ") & Environment.NewLine
        Dim strarr As String()
        Dim ctlline As New TextBox
        ctlline.Multiline = True
        ctlline.Lines = alamat.Split(vbCrLf)
        strarr = ctlline.Lines
        ctlline.Text = ""
        If strarr.Length > 0 Then
            For Each s As String In strarr
                If Not s = "" Then
                    dataprint &= Strings.Space(iLeftMargin) & printer.FillCenter(s, 31, " ") & Environment.NewLine
                End If
            Next
        End If

        'dataprint &= Strings.Space(iLeftMargin) & printer.FillCenter(alamat, 31, " ") & Environment.NewLine
        dataprint &= Strings.Space(iLeftMargin) & printer.FillCenter(notelp, 31, " ") & Environment.NewLine
        dataprint &= Strings.Space(iLeftMargin) & printer.FillCenter("-------------------------------", 31, " ") & Environment.NewLine
        If dt IsNot Nothing Then
            If dt.Rows.Count > 0 Then
                dataprint &= Strings.Space(iLeftMargin) & printer.Fill("Nota Penjualan", 31, False, False, " ", False, False) & Environment.NewLine
                dataprint &= Strings.Space(iLeftMargin) & printer.Fill("NoTRX    : " & otid, 31, False, False, " ", False, False) & Environment.NewLine
                dataprint &= Strings.Space(iLeftMargin) & printer.Fill("Tanggal  : " & dt(0)("tanggal").ToString, 31, False, False, " ", False, False) & Environment.NewLine
                dataprint &= Strings.Space(iLeftMargin) & printer.Fill("Waitress  : " & dt(0)("idwaitress").ToString, 31, False, False, " ", False, False) & Environment.NewLine
                dataprint &= Strings.Space(iLeftMargin) & printer.Fill("Meja No  : " & dt(0)("idtabel").ToString, 31, False, False, " ", False, False) & Environment.NewLine
                dataprint &= Strings.Space(iLeftMargin) & printer.FillCenter("-------------------------------", 31, " ") & Environment.NewLine
                For Each dr As DataRow In dt.Rows

                    dataprint &= Strings.Space(iLeftMargin) & printer.Fill(dr("orderno") & ". " & dr("namaitemmenu") & "(" & dr("iditemmenu") & ")", 31, False, False, " ", False, False) & Environment.NewLine
                    dataprint &= Strings.Space(iLeftMargin) & _
                    printer.Fill(dr("qty"), 4, True, True, " ", False, False) & _
                printer.Fill(" " & dr("harga"), 13, True, True, " ", True, False) & _
                printer.Fill(dr("totaldetail"), 14, True, True, " ", True, False) & _
                 Environment.NewLine
                Next
                dataprint &= Strings.Space(iLeftMargin) & printer.FillCenter("-------------------------------", 31, " ") & Environment.NewLine
                dataprint &= Strings.Space(iLeftMargin) & _
                            printer.Fill("SubTotal", 12, False, False, " ", False, False) & _
                        printer.Fill(dt(0)("subtotal"), 19, True, True, " ", True, False) & _
                         Environment.NewLine
                dataprint &= Strings.Space(iLeftMargin) & _
                            printer.Fill("Disc", 12, False, False, " ", False, False) & _
                        printer.Fill(dt(0)("discrp"), 19, True, True, " ", True, False) & _
                         Environment.NewLine
                dataprint &= Strings.Space(iLeftMargin) & _
                            printer.Fill("Total", 12, False, False, " ", False, False) & _
                        printer.Fill(dt(0)("total"), 19, True, True, " ", True, False) & _
                         Environment.NewLine
                dataprint &= Strings.Space(iLeftMargin) & printer.FillCenter("-------------------------------", 31, " ") & Environment.NewLine
                dataprint &= Strings.Space(iLeftMargin) & _
                            printer.Fill("Bayar", 12, False, False, " ", False, False) & _
                        printer.Fill(dt(0)("bayar"), 19, True, True, " ", True, False) & _
                         Environment.NewLine
                dataprint &= Strings.Space(iLeftMargin) & _
                            printer.Fill("Kembali", 12, False, False, " ", False, False) & _
                        printer.Fill(dt(0)("kembali"), 19, True, True, " ", True, False) & _
                         Environment.NewLine
            End If
        End If
        dataprint &= Environment.NewLine
        dataprint &= Strings.Space(iLeftMargin) & printer.Fill(tanggaljam, 31, False, False, " ", False, False) & Environment.NewLine
        dataprint &= Strings.Space(iLeftMargin) & printer.Fill("Petugas : " & Session.username, 31, False, False, " ", False, False) & Environment.NewLine
        dataprint &= Strings.Space(iLeftMargin) & printer.FillCenter("******************************", 31, " ") & Environment.NewLine
        dataprint &= Strings.Space(iLeftMargin) & printer.FillCenter("*********TERIMA KASIH*********", 31, " ") & Environment.NewLine
        dataprint &= Constants.vbFormFeed
        txtBill.Text = dataprint
    End Sub

    Private Sub HoverMeja(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim btndim As New Button
        btndim = CType(sender, Button)
        If btndim.BackColor = Color.Green Then
            txtBill.Text = "Meja No :" & btndim.Text
            viewBill(btndim.Text)
        End If
    End Sub

    Private Sub createMenu()
        Dim dtmenu As New DataTable
        Dim dtmenudetail As New DataTable
        Dim groups As Hashtable = New Hashtable
        If Utils.executeSP("proc_itemmenugroup", New Object() {"select", Convert.DBNull, Convert.DBNull}, dtmenu) Then
            If dtmenu.Rows.Count > 0 Then
                If LVMenu IsNot Nothing Then
                    LVMenu.View = View.LargeIcon 'defaulted, change to user saved
                    LVMenu.HoverSelection = True
                    LVMenu.HotTracking = True
                    LVMenu.Activation = ItemActivation.OneClick
                    LVMenu.ShowItemToolTips = True
                    LVMenu.Groups.Clear()
                    'AddHandler LVMenu.ItemActivate, AddressOf activateActivity

                End If
                Dim iico As Integer = -1
                For Each row As DataRow In dtmenu.Rows

                    If LVMenu IsNot Nothing Then
                        Dim group As ListViewGroup = New ListViewGroup(row("namagroupmenu"))
                        LVMenu.Groups.Add(group)
                        groups.Add("" & row("groupmenu"), group)
                    End If
                    dtmenudetail = New DataTable
                    If Utils.executeSP("proc_searchallEqual", New Object() {"iditemmenu,namaitemmenu,groupmenu,harga,imageid", "vitemmenu", "groupmenu", row("groupmenu")}, dtmenudetail) Then

                        For Each dr As DataRow In dtmenudetail.Rows
                            iico += 1
                            If LVMenu IsNot Nothing Then

                                Dim item As ListViewItem = LVMenu.Items.Add(dr("namaitemmenu"), IIf(IsDBNull(dr("imageid")), iico, dr("imageid")))
                                Dim groupId As String = dr("groupmenu")
                                'groupId = groupId.Substring(0, 1)
                                item.Tag = dr("iditemmenu")
                                item.Group = groups(groupId)

                                'Dim itemname As String = dr("namaitemmenu") & " " & dr("harga")
                                Dim itemname As String = "Detail Menu : "
                                Dim dtd As New DataTable
                                dtd = GetAllEqual("vitemmenudetail", "iditemmenu", dr("iditemmenu"))
                                If Utils.RowExist(dtd) Then
                                    For Each rd As DataRow In dtd.Rows
                                        itemname += vbCrLf & rd("namaproduk") & vbTab & rd("qtydetail")
                                    Next
                                End If
                                itemname += vbCrLf & "----------"
                                itemname += vbCrLf & "Harga" & vbTab & Format(dr("harga"), "###,###")
                                dtd = New DataTable
                                dtd = GetAllEqual("vstokmenufix", "iditemmenu", dr("iditemmenu"))
                                If Utils.RowExist(dtd) Then
                                    For Each rd As DataRow In dtd.Rows
                                        itemname += vbCrLf & "Stok" & vbTab & rd("stok")
                                    Next
                                End If

                                item.ToolTipText = itemname
                                'item.SubItems.Add(row("description"))
                            End If
                            If iico = 7 Then
                                iico = -1
                            End If
                        Next
                    End If

                Next
                For Each g As ListViewGroup In LVMenu.Groups
                    If g.Header.ToLower = "default" Then
                        MsgBox(g.Header)
                        Exit For
                    End If

                Next
            End If
        End If
    End Sub

    Private Sub RefreshWaiters()
        Dim dt As New DataTable
        If Utils.executeSP("proc_searchall", New Object() {"*", "vwaiters", "username", ""}, dt) Then
            If Utils.RowExist(dt) Then
                cmbwaiters.DataSource = dt
                cmbwaiters.DisplayMember = "username"
                cmbwaiters.ValueMember = "username"
            End If
        End If
    End Sub



    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

    End Sub

    Private Sub TrxOrderJualPage_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        RefreshTabel()
        createMenu()
        RefreshWaiters()
    End Sub

    Private Sub TrxOrderJualPage_VisibleChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.VisibleChanged
        RefreshTabel()
        createMenu()
        RefreshWaiters()
    End Sub

    Private Sub AktifgridToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AktifgridToolStripMenuItem.Click
        GridTransaksi.Focus()
    End Sub

    Private Sub GridTransaksi_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles GridTransaksi.KeyDown
        If e.KeyValue = Keys.Delete Then
            Try
                Dim dgr As New DataGridViewRow
                dgr = GridTransaksi.CurrentRow
                GridTransaksi.Rows.Remove(dgr)
            Catch ex As Exception

            End Try
        End If
    End Sub

    Private Function validasirowgrid(ByVal dgr As DataGridView) As Boolean
        Dim retval As Boolean = True
        If GridTransaksi.RowCount > 0 Then
            If GridTransaksi.RowCount = 1 And GridTransaksi.Rows(0).IsNewRow Then
                retval = False
            End If
        Else
            retval = False
        End If
        Return retval


    End Function

    Private Sub sumtotal()
        'count item
        If GridTransaksi.Rows.Count > 0 Then
            Dim i As Integer = 0
            Dim d As Double = 0
            i = GridTransaksi.Rows.GetLastRow(DataGridViewElementStates.None)
            If GridTransaksi.Rows(i).IsNewRow Then
                txtTotalItem.Text = GridTransaksi.Rows.Count - 1
                d = 0
                For ir As Integer = 0 To GridTransaksi.Rows.Count - 1
                    d += GridTransaksi("total", ir).Value
                Next
                txtTotal.Text = d.ToString
            Else
                txtTotalItem.Text = GridTransaksi.Rows.Count
                d = 0
                For ir As Integer = 0 To GridTransaksi.Rows.Count
                    d += GridTransaksi("total", ir).Value
                Next
                txtTotal.Text = d.ToString
            End If
        ElseIf GridTransaksi.Rows.Count = 0 Then
            txtTotalItem.Text = "0"
            txtTotal.Text = "0"
        End If

    End Sub

    Private Sub sumhutang()
        Dim total As Double
        Dim subtotal As Double
        'Dim totalg As Double
        Try
            total = IIf(txtTotal.NumericText = String.Empty, 0, txtTotal.NumericText)
            subtotal = total - CDbl(txtDiscRp.NumericText)
            txtsubtotal.Text = CStr(subtotal)

            'If subtotal > CDbl(txtBayar.NumericText) Or subtotal = CDbl(txtBayar.NumericText) Then
            txtHutang.Text = subtotal - CDbl(txtBayar.NumericText)
            'Else
            'txtHutang.Text = 0
            'End If
        Catch ex As Exception

        End Try


    End Sub

    Private Sub GridTransaksi_EditingControlShowing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles GridTransaksi.EditingControlShowing
        If GridTransaksi.CurrentCell.ColumnIndex = 3 Then
            Dim TextBox1 As TextBox
            TextBox1 = CType(e.Control, TextBox)
            RemoveHandler TextBox1.KeyPress, New KeyPressEventHandler(AddressOf TextBox_KeyPress)
            AddHandler TextBox1.KeyPress, New KeyPressEventHandler(AddressOf TextBox_KeyPress)
            ''ElseIf GridTransaksi.CurrentCell.ColumnIndex = 0 Then
            ' ''    Dim TextBox1 As TextBox
            ''    TextBox1 = CType(e.Control, TextBox)
            ''    RemoveHandler TextBox1.KeyPress, New KeyPressEventHandler(AddressOf TextBox_KeyPress)
            ''    AddHandler TextBox1.KeyPress, New KeyPressEventHandler(AddressOf TextBox1_KeyPress)
        End If
    End Sub

    Private Sub TextBox_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        e.Handled = Utils.HanyaAngka(Asc(e.KeyChar))
    End Sub

    Private Function GetAllEqual(ByVal tblname As String, ByVal finder As String, ByVal finderkey As String) As DataTable
        Dim dt As New DataTable
        Dim params As Object()
        Params = New Object() {"*", tblname, finder, finderkey}
        If Utils.executeSP("proc_searchallEqual", Params, dt) Then

        End If
        Return dt
    End Function

    Private Sub GridTransaksi_CellEndEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles GridTransaksi.CellEndEdit
        If GridTransaksi.CurrentCell.ColumnIndex = 0 Then
            If Not IsDBNull(GridTransaksi.CurrentCell) Then
                If GridTransaksi.CurrentCell.Value IsNot Nothing Then
                    'blm kelar=? masukkan search equal
                    Dim dt As New DataTable
                    dt = GetAllEqual("vitemmenu", "iditemmenu", GridTransaksi.CurrentCell.Value)
                    If Utils.RowExist(dt) Then
                        GridTransaksi(1, e.RowIndex).Value = IIf(IsDBNull(dt(0)("namaitemmenu")), "", dt(0)("namaitemmenu"))
                        GridTransaksi(2, e.RowIndex).Value = IIf(IsDBNull(dt(0)("harga")), "", dt(0)("harga"))
                        GridTransaksi(3, e.RowIndex).Value = IIf(IsDBNull(dt(0)("qty")), 0, dt(0)("qty"))
                        'GridTransaksi(4, e.RowIndex).Value = IIf(IsDBNull(dt(0)("satuan")), "", dt(0)("satuan"))
                        GridTransaksi(5, e.RowIndex).Value = GridTransaksi(2, e.RowIndex).Value * GridTransaksi(3, e.RowIndex).Value
                        sumtotal()
                        sumhutang()
                    Else
                        MsgBox("data tidak terdaftar")
                        GridTransaksi.Rows.RemoveAt(e.RowIndex)
                        GridTransaksi.CurrentCell = GridTransaksi.Item(0, e.RowIndex)
                        GridTransaksi.Item(0, e.RowIndex).Selected = True
                    End If
                End If
            End If
            For Each dgr As DataGridViewRow In GridTransaksi.Rows
                dgr.Cells(6).Value = dgr.Index + 1
                'If dgr.Index <> GridTransaksi.CurrentCell.RowIndex Then
                '    If dgr.Cells(0).Value = GridTransaksi.CurrentCell.Value Then
                '        dgr.Cells(3).Value = dgr.Cells(3).Value + 1
                '        dgr.Cells(5).Value = dgr.Cells(3).Value * dgr.Cells(2).Value

                '        GridTransaksi.Rows.Remove(GridTransaksi.CurrentRow)
                '        sumtotal()
                '        sumhutang()
                '    End If
                'End If

            Next
        ElseIf GridTransaksi.CurrentCell.ColumnIndex = 3 Then
            If Not IsDBNull(GridTransaksi.CurrentCell) Then
                If GridTransaksi.CurrentCell.Value IsNot Nothing Then
                    GridTransaksi(5, e.RowIndex).Value = GridTransaksi(2, e.RowIndex).Value * GridTransaksi(3, e.RowIndex).Value
                    sumtotal()
                    sumhutang()
                End If
            End If
        End If
    End Sub

    Private Sub GridTransaksi_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles GridTransaksi.KeyUp
        Dim ic, ir, qtytambah As Integer
        If validasirowgrid(GridTransaksi) Then
            ic = GridTransaksi.CurrentCell.ColumnIndex
            ir = GridTransaksi.CurrentCell.RowIndex
        Else
            Exit Sub
        End If

        If e.KeyValue = 13 Then
            If GridTransaksi.CurrentCell.ColumnIndex = 1 Or GridTransaksi.CurrentCell.ColumnIndex = 2 Then
                If Not IsDBNull(GridTransaksi(0, ir).Value) Or Not IsDBNull(GridTransaksi.CurrentCell.Value) Then
                    If GridTransaksi(0, ir).Value IsNot Nothing Or GridTransaksi(1, ir).Value IsNot Nothing Then
                        If Not IsDBNull(GridTransaksi(3, ir).Value) Then
                            If GridTransaksi(3, ir).Value = 0 Then
                                qtytambah = 0
                                Dim dt As New DataTable
                                dt = GetAllEqual("vitemmenu", "iditemmenu", GridTransaksi(0, ir).Value)
                                If Utils.RowExist(dt) Then
                                    qtytambah = dt(0)("qty")
                                End If
                                GridTransaksi(3, ir).Value = qtytambah
                                GridTransaksi(5, ir).Value = GridTransaksi(3, ir).Value * GridTransaksi(2, ir).Value
                                sumtotal()
                                sumhutang()
                                GridTransaksi.CurrentCell = GridTransaksi(0, GridTransaksi.RowCount - 1)
                                GridTransaksi.CurrentCell.Selected = True
                            ElseIf GridTransaksi(3, ir).Value > 0 Then

                                GridTransaksi.CurrentCell = GridTransaksi(3, ir)
                                GridTransaksi.CurrentCell.Selected = True
                            End If
                        Else
                            GridTransaksi.CurrentCell = GridTransaksi(3, ir)
                            GridTransaksi.CurrentCell.Selected = True
                        End If

                    Else
                        GridTransaksi.CurrentCell = GridTransaksi(0, GridTransaksi.RowCount - 1)
                        GridTransaksi.CurrentCell.Selected = True
                    End If
                Else
                    GridTransaksi.CurrentCell = GridTransaksi(0, GridTransaksi.RowCount - 1)
                    GridTransaksi.CurrentCell.Selected = True
                End If

                'Dim eKey As New KeyEventArgs(Keys.Return)
                'GridTransaksi_KeyUp(sender, eKey)
            ElseIf GridTransaksi.CurrentCell.ColumnIndex = 4 Or GridTransaksi.CurrentCell.ColumnIndex = 5 Then
                GridTransaksi.CurrentCell = GridTransaksi(0, GridTransaksi.RowCount - 1)
                GridTransaksi.CurrentCell.Selected = True
            End If
        End If
    End Sub

    Private Sub GridTransaksi_RowsRemoved(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowsRemovedEventArgs) Handles GridTransaksi.RowsRemoved
        sumtotal()
        sumhutang()
        For Each dgr As DataGridViewRow In GridTransaksi.Rows
            dgr.Cells(6).Value = dgr.Index + 1
        Next
    End Sub

    Private Sub FocusupplierToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FocusupplierToolStripMenuItem.Click
        cmbMeja.Focus()
    End Sub

    Private Sub SimpanToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpanToolStripMenuItem.Click
        If cmbMeja.Text = "" Then
            MsgBox("Data Entry Tidak Valid")
            Exit Sub
        End If
        If cmbwaiters.SelectedValue.ToString = "" Then
            MsgBox("Data Entry Tidak Valid")
            Exit Sub
        End If
        If txtTotalItem.NumericText = 0 Then
            MsgBox("Data Entry Tidak Valid")
            Exit Sub
        End If
        If GridTransaksi.Rows(0).IsNewRow Then
            MsgBox("Data Entry Tidak Valid")
            Exit Sub
        End If
        GridTransaksi.CommitEdit(DataGridViewDataErrorContexts.Commit)
        'cek status meja
        'jika pasif mk simpan
        'jika aktif mk dialog close/edit
        Dim dt As New DataTable
        dt = GetAllEqual("vtabelaktif", "idtabel", cmbMeja.Text)
        If Not Utils.RowExist(dt) Then
            'simpan order 
            Dim retvalarr As New List(Of String)
            Dim dtstok As New DataTable
            Dim vstok As Double
            retvalarr.Clear()
            For Each dgr As DataGridViewRow In GridTransaksi.Rows
                dtstok = New DataTable
                If Not dgr.IsNewRow Then
                    If Not dgr.Cells("qty").ReadOnly Then
                        If Utils.GetFunction("getstokmenu", New Object() {CInt(dgr.Cells("iditem").Value)}, dtstok) Then
                            If Utils.RowExist(dtstok) Then
                                vstok = dtstok(0)(0)
                                If vstok > 0 Then
                                    If vstok < dgr.Cells("qty").Value Then
                                        retvalarr.Add(dgr.Cells("namaitem").Value & " -> " & "Stok " & vstok & _
                                                      " Tidak Mencukupi")
                                    End If
                                End If
                            End If
                        End If
                    End If
                End If
            Next
            If retvalarr.Count > 0 Then
                Dim retmsg As String = ""
                For Each smsg As String In retvalarr
                    retmsg &= vbCrLf & smsg
                Next
                MsgBox("Stok Menu Tidak Mencukupi" & retmsg)
                Exit Sub
            End If
            Dim otid As String
            Dim dtfunc As New DataTable
            If Utils.GetFunction("getidtrx", New Object() {"ot"}, dtfunc) Then
                If Utils.RowExist(dtfunc) Then
                    otid = dtfunc(0)(0)
                    If Utils.exec_SP("proc_opentabel", New Object() {otid, Now.Date.ToString("yyyy-MM-dd"), cmbMeja.Text, cmbwaiters.Text, LocalSession.operatorname}) Then

                        If retvalarr.Count = 0 Then
                            For Each dgr As DataGridViewRow In GridTransaksi.Rows
                                If Not dgr.IsNewRow Then
                                    If Utils.exec_SP("proc_orderjualdetail", _
                                                     New Object() {"insert", otid, dgr.Cells("iditem").Value, _
                                                                   dgr.Cells("namaitem").Value, _
                                                                   dgr.Cells("harga").Value, _
                                                                   dgr.Cells("qty").Value, _
                                                                   dgr.Cells("total").Value, _
                                                                   dgr.Cells("No").Value}) Then

                                    End If
                                End If
                            Next
                            If Utils.exec_SP("proc_orderjualtrx", New Object() {"update", otid, txtTotal.NumericText, txtDiscPersen.NumericText, txtDiscRp.NumericText, txtsubtotal.NumericText, txtBayar.NumericText, txtHutang.NumericText}) Then

                            End If
                            If Utils.exec_SP("proc_tabel", New Object() {"setaktif", cmbMeja.Text, Convert.DBNull, Convert.DBNull, 1}) Then
                                RefreshTabel()
                            End If
                            GridTransaksi.Rows.Clear()
                            MsgBox("Execute Success")
                            TabControl1.SelectedIndex = 0
                            'Dim strdata As String = ""
                            'strdata = Utils.GridValueToText(GridTransaksi)
                            'Dim params As String()

                            'If Not strdata = "" Then
                            '    params = strdata.Split("|")
                            '    For int As Integer = 0 To params.Length - 1
                            '        params(int) = params(int).Insert(0, txtnopo.Text & ",")
                            '    Next
                            '    strdata = String.Join("|", params)
                            '    If strdata.EndsWith("|") Then
                            '        strdata = LSet(strdata, strdata.Length - 1)
                            '    End If
                            '    If Utils.exec_SP("proc_orderbelid", New Object() {txtnopo.Text, txtTotalItem.NumericText, strdata}) Then

                            '    End If
                            'End If
                        End If

                    End If
                End If
            End If

        Else
            'edit / close
            Dim retvalarr As New List(Of String)
            Dim dtstok As New DataTable
            Dim vstok As Double
            retvalarr.Clear()
            For Each dgr As DataGridViewRow In GridTransaksi.Rows
                dtstok = New DataTable
                If Not dgr.IsNewRow Then
                    If Not dgr.Cells("qty").ReadOnly Then
                        If Utils.GetFunction("getstokmenu", New Object() {dgr.Cells("iditem").Value}, dtstok) Then
                            If Utils.RowExist(dtstok) Then
                                vstok = dtstok(0)(0)
                                If vstok > 0 Then
                                    If vstok < dgr.Cells("qty").Value Then
                                        retvalarr.Add(dgr.Cells("namaitem").Value & " -> " & "Stok " & vstok & _
                                                      " Tidak Mencukupi")
                                        GridTransaksi.Rows.Remove(dgr)
                                    End If
                                Else
                                    retvalarr.Add(dgr.Cells("namaitem").Value & " -> " & "Stok " & vstok & _
                                                      " Tidak Mencukupi")
                                    GridTransaksi.Rows.Remove(dgr)
                                End If
                            End If
                        End If
                    End If
                End If
            Next
            If retvalarr.Count > 0 Then
                Dim retmsg As String = ""
                For Each smsg As String In retvalarr
                    retmsg &= vbCrLf & smsg
                Next
                MsgBox("Stok Menu Tidak Mencukupi" & retmsg)
                Exit Sub
            End If
            Dim otid As String
            Dim dtfunc As New DataTable
            If Utils.GetFunction("getidtrxopen", New Object() {cmbMeja.Text}, dtfunc) Then
                If Utils.RowExist(dtfunc) Then
                    otid = dtfunc(0)(0)

                    If retvalarr.Count = 0 Then
                        For Each dgr As DataGridViewRow In GridTransaksi.Rows
                            If Not dgr.IsNewRow Then
                                If Not dgr.Cells("qty").ReadOnly Then


                                    If Utils.exec_SP("proc_orderjualdetail", _
                                                     New Object() {"insert", otid, dgr.Cells("iditem").Value, _
                                                                   dgr.Cells("namaitem").Value, _
                                                                   dgr.Cells("harga").Value, _
                                                                   dgr.Cells("qty").Value, _
                                                                   dgr.Cells("total").Value, _
                                                                   dgr.Cells("No").Value}) Then

                                    End If
                                End If
                            End If
                        Next
                        If Utils.exec_SP("proc_orderjualtrx", New Object() {"update", otid, txtTotal.NumericText, txtDiscPersen.NumericText, txtDiscRp.NumericText, txtsubtotal.NumericText, txtBayar.NumericText, txtHutang.NumericText}) Then

                        End If

                        GridTransaksi.Rows.Clear()
                        MsgBox("Execute Success")
                        TabControl1.SelectedIndex = 0
                    End If
                End If
            End If
        End If



    End Sub

    Private Sub LVMenu_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles LVMenu.MouseDoubleClick
        Dim item As ListViewItem
        Try
            item = LVMenu.SelectedItems(0)
            If item.Tag Is Nothing Then
                MsgBox("Application Error : no page defined for action : " & item.Tag)
            Else
                Dim dt As New DataTable
                dt = GetAllEqual("vitemmenu", "iditemmenu", item.Tag)
                If Utils.RowExist(dt) Then
                    MsgBox(dt(0)("iditemmenu").ToString & " - " & dt(0)("namaitemmenu").ToString & vbCrLf & dt(0)("harga").ToString)

                    GridTransaksi.Rows.Add()
                    GridTransaksi(0, GridTransaksi.RowCount - 2).Value = item.Tag
                    'GridTransaksi(1, GridTransaksi.RowCount - 2).Value = IIf(IsDBNull(dt(0)("namaitemmenu")), "", dt(0)("namaitemmenu"))
                    'GridTransaksi(3, GridTransaksi.RowCount - 2).Value = 1
                    GridTransaksi.Focus()
                    GridTransaksi.CurrentCell = GridTransaksi(0, GridTransaksi.RowCount - 2)
                    GridTransaksi(0, GridTransaksi.RowCount - 2).Selected = True
                    GridTransaksi.BeginEdit(True)
                    GridTransaksi.EndEdit(True)
                End If

            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub cmbMeja_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbMeja.SelectedIndexChanged
        Dim tblno As String = ""
        tblno = cmbMeja.SelectedIndex + 1
        Dim dt As New DataTable
        dt = GetAllEqual("vtabelaktif", "idtabel", tblno)
        GridTransaksi.Rows.Clear()
        If Utils.RowExist(dt) Then
            Dim dtitem As New DataTable
            dtitem = GetAllEqual("vorderjualaktif", "idtabel", tblno)
            Dim i As Integer = 0
            If Utils.RowExist(dtitem) Then
                For Each ritem As DataRow In dtitem.Rows
                    GridTransaksi.Rows.Add()
                    i += 1
                    Dim irowactive As Integer = GridTransaksi.RowCount - 2
                    GridTransaksi("iditem", irowactive).Value = ritem("iditemmenu")
                    GridTransaksi("namaitem", irowactive).Value = ritem("namaitemmenu")
                    GridTransaksi("harga", irowactive).Value = ritem("harga")
                    GridTransaksi("qty", irowactive).Value = ritem("qty")
                    GridTransaksi("qty", irowactive).ReadOnly = True
                    GridTransaksi("total", irowactive).Value = ritem("subtotal")
                    GridTransaksi("No", irowactive).Value = i
                    sumtotal()
                    sumhutang()
                Next

            End If
        End If
    End Sub

    Private Sub cbxDiscPersen_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbxDiscPersen.CheckedChanged
        If cbxDiscPersen.Checked Then
            txtDiscPersen.Enabled = True
            txtDiscPersen_TextChanged(sender, e)
        Else

            txtDiscPersen.Text = 0
            Dim rp As Double
            rp = 0
            Try
                rp = CDbl(txtTotal.NumericText) * (CDbl(txtDiscPersen.Text) / 100)
            Catch ex As Exception

            End Try

            txtDiscRp.Text = CStr(rp)
            sumhutang()
            txtDiscPersen.Enabled = False
        End If
    End Sub

    Private Sub txtDiscPersen_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDiscPersen.TextChanged
        If cbxDiscPersen.Checked Then
            Dim rp As Double
            rp = 0
            Try
                rp = CDbl(txtTotal.NumericText) * (CDbl(txtDiscPersen.Text) / 100)
            Catch ex As Exception

            End Try

            txtDiscRp.Text = CStr(rp)
            sumhutang()
        End If
    End Sub

    Private Sub BayarToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BayarToolStripMenuItem.Click
        If CDbl(txtsubtotal.NumericText) <= 0 Then
            Exit Sub
        End If
        Dim retval As Boolean = True
        If txtTotalItem.NumericText = 0 Then
            retval = False
        End If
        If Not retval Then
            MsgBox("Maaf Data Entry Tidak Valid")
            Exit Sub
        End If
        Dim otid As String = ""
        Dim dtfunc As New DataTable
        If Utils.GetFunction("getidtrxopen", New Object() {cmbMeja.Text}, dtfunc) Then
            If Utils.RowExist(dtfunc) Then
                otid = dtfunc(0)(0)
            End If
        End If
        If otid = "" Then
            MsgBox("Maaf Transaksi Tidak Terdaftar")
            Exit Sub
        End If
        Dim dtcek As New DataTable
        dtcek = GetAllEqual("vnotajual", "idtrx", otid)
        If Utils.RowExist(dtcek) Then
            MsgBox("Maaf Transaksi Sudah dibayar")
            Exit Sub
        End If
        dtcek = New DataTable
        dtcek = GetAllEqual("vorderjualaktif", "idtrx", otid)
        If Not Utils.RowExist(dtcek) Then
            MsgBox("Maaf Transaksi Tidak Terdaftar")
            Exit Sub
        End If
        Dim DialogBayarAll As New DialogBayarAll
        DialogBayarAll.txtTotal.Text = txtsubtotal.NumericText
        DialogBayarAll.parentme = Me.Name
        DialogBayarAll.ShowDialog()
        If DialogBayarAll.returnbayar Then
            txtBayar.Text = DialogBayarAll.txtBayar.NumericText
            lblnotrxbank.Text = DialogBayarAll.ComBank.Text
            lblnotrxbank.Text = DialogBayarAll.txtnumbank.Text
            sumhutang()
        End If

        If MessageBox.Show("Pembayaran Cash Meja NO : " & cmbMeja.Text & "  No TRX : " & otid & vbCrLf & _
                               "Jumlah Yg dibayar " & vbTab & " = " & txtsubtotal.Text & vbCrLf & _
                               "Jumlah Pembayaran " & vbTab & " = " & txtBayar.Text & vbCrLf & _
                               "Sisa / Kembali " & vbTab & " = " & txtHutang.Text & vbCrLf & vbCrLf _
                               & "Apakah Anda Akan Mencetak & Menyimpan Data ? " _
                               , "Informasi", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = DialogResult.Yes Then

            'cek(printer)
            If My.Settings.PrinterName = "" Then
                MsgBox("Eksekusi Gagal, Status Printer Tidak Tersetting")
                Exit Sub
            End If
            'execsimpan()
            If Utils.exec_SP("proc_orderjualtrx", New Object() {"bayar", otid, txtTotal.NumericText, txtDiscPersen.NumericText, txtDiscRp.NumericText, txtsubtotal.NumericText, txtBayar.NumericText, txtHutang.NumericText}) Then
                If Utils.exec_SP("proc_setstokexec", New Object() {otid}) Then

                End If
                Dim dt As New DataTable
                Dim clsprint As New ClassAll.Class.ClsOutPutTransaksi
                dt = GetAllEqual("vnotajual", "idtrx", otid)
                If Utils.RowExist(dt) Then
                    clsprint.cetakNotaJual(dt, otid)
                End If
                GridTransaksi.Rows.Clear()
                txtBayar.Text = 0
                sumtotal()
                sumhutang()
                RefreshTabel()
            End If


        End If
    End Sub

    Private Sub txtDiscRp_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtDiscRp.KeyUp
        If txtDiscRp.NumericText = String.Empty Then
            txtDiscRp.Text = "0"

            'ElseIf txtnopo.Text = String.Empty Then
            '    txtDiscRp.Text = 0
        ElseIf txtTotal.NumericText = String.Empty Or txtTotal.NumericText = "0" Then
            txtDiscRp.Text = "0"
        End If
        'If cbxDiscPersen.Checked Then
        '    Dim rp As Double
        '    rp = CDbl(GridControl1.txtTotal.NumericText) * (CDbl(txtDiscPersen.Text) / 100)
        '    txtDiscRp.Text = CStr(rp)
        'End If
        sumhutang()
    End Sub

    Private Sub CekstokToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CekstokToolStripMenuItem.Click
        Dim DialogCekStok As New DialogCekStok
        DialogCekStok.ShowDialog()
    End Sub

End Class
